package cn.itsource.blog.permission;

import cn.hutool.core.util.ClassUtil;
import cn.itsource.blog.anno.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Set;

@Component
public class PermissionScan {

    String rootPath = "cn/itsource/blog";

    public void scan(){
        URL url = this.getClass().getResource("/");
        System.out.println(url);
        File file = new File(url.toString(), rootPath);

        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.isDirectory();
            }
        });


        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation("cn.itsource.blog.controller", RestController.class);
        for (Class<?> clz : classes) {
            RequestMapping annotation = clz.getAnnotation(RequestMapping.class);
            String[] value = annotation.value();
            String path = value[0];

            Method[] methods = clz.getMethods();
            for (Method method : methods) {
                PreAuthorize preAuthorize = method.getAnnotation(PreAuthorize.class);
                if(preAuthorize == null){
                    continue;
                }
                PutMapping putMapping = method.getAnnotation(PutMapping.class);
                if(putMapping!=null){
                    String[] paths = putMapping.value();
                    if(paths!=null && paths.length!=0){
                        String path1 = paths[0];
                        if(path1.indexOf("/")==-1){
                            path = path + "/" + paths[0];
                        }else{
                            path = path +paths[0];
                        }
                    }
                }

                PostMapping postMapping = method.getAnnotation(PostMapping.class);
                if(postMapping!=null){

                    String[] paths = postMapping.value();
                    if(paths!=null && paths.length!=0){
                        String path1 = paths[0];
                        if(path1.indexOf("/")==-1){
                            path = path + "/" + paths[0];
                        }else{
                            path = path +paths[0];
                        }
                    }
                }

                PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
                if(patchMapping!=null){
                    String[] paths = patchMapping.value();
                    if(paths!=null && paths.length!=0){
                        String path1 = paths[0];
                        if(path1.indexOf("/")==-1){
                            path = path + "/" + paths[0];
                        }else{
                            path = path +paths[0];
                        }
                    }
                }


                DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
                if(deleteMapping!=null){
                    String[] paths = deleteMapping.value();
                    if(paths!=null && paths.length!=0){
                        String path1 = paths[0];
                        if(path1.indexOf("/")==-1){
                            path = path + "/" + paths[0];
                        }else{
                            path = path +paths[0];
                        }
                    }
                }

                System.out.println(path);

            }

        }


    }
}
